package com.manlu.service;

import com.manlu.config.JwtProperties;
import com.manlu.mapper.AdminMapper;
import com.manlu.pojo.Admin;
import com.manlu.utils.JwtUtils;
import com.manlu.vo.BaseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Auther 漫路h
 * Created by 2020-05-04 20:48
 */
@Transactional
@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Resource
    private JwtProperties jwtProperties;


    /**
     * 管理员登录
     * @param admin
     * @return
     */
    public BaseResult login(Admin admin) {
        Admin loginAdmin = adminMapper.selectAdminByUsername(admin.getUsername());
        if (loginAdmin!=null){
            //查询到用户
            if (loginAdmin.getPassword().equals(admin.getPassword())) {
                //登录成功
                //加密token,返回
                String token = JwtUtils.generateToken(loginAdmin, jwtProperties.getExpire(), jwtProperties.getPrivateKey());
                return BaseResult.ok("登录成功").append("token",token);
            }
            return BaseResult.error("密码错误");
        }
        return BaseResult.error("用户名不存在");
    }
}
